// Author: Travis Fischer
// License: MIT
//
// Adapted from a Codrops article by Robin Delaporte
// https://tympanus.net/Development/DistortionHoverEffect

#include <metal_stdlib>
#include "MTIShaderLib.h"
#include "MTTransitionLib.h"

using namespace metalpetal;

fragment float4 DisplacementFragment(VertexOut vertexIn [[ stage_in ]],
                                     texture2d<float, access::sample> fromTexture [[ texture(0) ]],
                                     texture2d<float, access::sample> toTexture [[ texture(1) ]],
                                     texture2d<float, access::sample> displacementMap [[ texture(2)]],
                                     constant float & strength [[ buffer(0) ]],
                                     constant float & ratio [[ buffer(1) ]],
                                     constant float & progress [[ buffer(2) ]],
                                     sampler textureSampler [[ sampler(0) ]])
{
    float2 uv = vertexIn.textureCoordinate;
    uv.y = 1.0 - uv.y;
    float _fromR = float(fromTexture.get_width())/float(fromTexture.get_height());
    float _toR = float(toTexture.get_width())/float(toTexture.get_height());
    constexpr sampler s(coord::normalized, address::clamp_to_edge, filter::linear);
    
    float displacement = displacementMap.sample(s, uv).r * strength;
    float2 uvFrom = float2(uv.x + progress * displacement, uv.y);
    float2 uvTo = float2(uv.x - (1.0 - progress) * displacement, uv.y);
    
    return mix(getFromColor(uvFrom, fromTexture, ratio, _fromR),
               getToColor(uvTo, toTexture, ratio, _toR),
               progress
               );
}

